package com.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ssm.pojo.User;
import com.ssm.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Cat.Sir
 * Description:
 * DateTime: 2022-04-30 20:46
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    @Qualifier("userService")
    private UserServiceImpl service;

    //登录判断用户是否存在以及判断用户的类型
    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response
            , @RequestParam("uName") String uName, @RequestParam("uPassword") String uPassword
            , HttpSession session) throws Exception {
        System.out.println(uName + "" + uPassword);
        User user = service.queryInfoByName(uName);
        if (user != null && uName != null && uPassword != null) {
            if (user.getUName().equals(uName) && user.getUPassword().equals(uPassword)) {
                session.setAttribute("uName", uName);
                boolean uType = false;
                if ("管理".equals(user.getUState())) {
                    uType = true;
                } else if ("正常".equals(user.getUState())) {
                    uType = false;
                }
                //将用户类型存入session中用于将对应的操作展示在页面中
                session.setAttribute("uType", uType);
                return "redirect:/book/allBook";
            }
//            if(user==null||uName==null||uPassword==null){
        }
        request.getRequestDispatcher("/index.jsp").forward(request, response);
        // response.sendRedirect("/index.jsp");
        return "";
    }

    //当用户成功登录后可以直接进入到主页面
    //否则拦截器生效跳转到登录页面
    @RequestMapping("/allBook")
    public String goAllBook() {

        return "redirect:/book/allBook";
    }

    //用户注销自己的登录信息
    @RequestMapping("/exit")
    public String cancel(HttpSession session, String uName) {
        session.removeAttribute("uName");
        return "redirect:/index.jsp";
    }

    //用户注册
    @RequestMapping("/register")
    @ResponseBody
    public String register(@RequestParam("uName") String uName, @RequestParam("pwd") String pwd) {
        String msg = "";
        User user = new User();
        user.setUName(uName);
        user.setUPassword(pwd);
        int flag = service.addUser(user);
        if (flag > 0) {
            msg = "恭喜您，注册成功！";
        } else {
            msg = "抱歉，注册失败！";
        }
        return msg;
    }

    //判断用户注册的信息是否可注册
    @RequestMapping("/check")
    @ResponseBody
    public String check(@RequestParam("uName") String uName) {
        String msg = "";
        System.out.println(uName);
        User user = service.queryInfoByName(uName);
        if (user != null) {
            msg = "已存在";
        } else {
            msg = "可注册";
        }
        return msg;
    }

    @RequestMapping("/findPwd")
    @ResponseBody
    public String findPwd(String userName) {
        List<User> users = service.queryAllInfo();
        for (User user : users) {
            boolean flag = user.getUName().equals(userName);
            System.out.println(flag + ":" + user.getUName() + userName);
            if (flag) {
                return "你的密码是：" + user.getUPassword();
            } else {
                return "用户名输入错误";
            }
        }
        return null;
    }

    @RequestMapping("/userInfo")
    public String goUserInfo(Model model, HttpSession session,HttpServletRequest request,HttpServletResponse response) {

        String uName = (String) session.getAttribute("uName");
        List<User> users = service.queryAllInfo();
        for (User user : users) {
            boolean equals = user.getUName().equals(uName);
            if(equals){
                model.addAttribute("uName",user.getUName());
                model.addAttribute("pwd",user.getUPassword());
                model.addAttribute("id",user.getId());
            }
        }
        return "userInfo";
    }

    @RequestMapping("/updatePwd")
    @ResponseBody
    public String updatePwd(int uId,String uPassword){
        User userUpdatePwd = new User();
        System.out.println(uId+""+uPassword);
        userUpdatePwd.setUPassword(uPassword);
        userUpdatePwd.setId(uId);
        String msg="修改失败";
        int row = service.updateUserPwdById(userUpdatePwd);
        if(row>0){
            msg="修改成功";
        }
        return msg;
    }
}
